package io.fredia.common.utils;

import java.io.Serializable;

import io.fredia.common.web.Md5Utils;

public class WeixiPayUtils {

    private class PayInfo implements Serializable {
        private static final long serialVersionUID = 5637164279924222380L;

        private String appid;
        private String mch_id;
        private String device_info;
        private String nonce_str;
        private String sign;
        private String body;
        private String attach;
        private String out_trade_no;
        private Integer total_fee;
        private String spbill_create_ip;
        private String notify_url;
        private String trade_type;
        private String openid;

        public String getAppid() {
            return appid;
        }

        public void setAppid(String appid) {
            this.appid = appid;
        }

        public String getMch_id() {
            return mch_id;
        }

        public void setMch_id(String mch_id) {
            this.mch_id = mch_id;
        }

        public String getDevice_info() {
            return device_info;
        }

        public void setDevice_info(String device_info) {
            this.device_info = device_info;
        }

        public String getNonce_str() {
            return nonce_str;
        }

        public void setNonce_str(String nonce_str) {
            this.nonce_str = nonce_str;
        }

        public String getSign() {
            return sign;
        }

        public void setSign(String sign) {
            this.sign = sign;
        }

        public String getBody() {
            return body;
        }

        public void setBody(String body) {
            this.body = body;
        }

        public String getAttach() {
            return attach;
        }

        public void setAttach(String attach) {
            this.attach = attach;
        }

        public String getOut_trade_no() {
            return out_trade_no;
        }

        public void setOut_trade_no(String out_trade_no) {
            this.out_trade_no = out_trade_no;
        }

        public Integer getTotal_fee() {
            return total_fee;
        }

        public void setTotal_fee(Integer total_fee) {
            this.total_fee = total_fee;
        }

        public String getSpbill_create_ip() {
            return spbill_create_ip;
        }

        public void setSpbill_create_ip(String spbill_create_ip) {
            this.spbill_create_ip = spbill_create_ip;
        }

        public String getNotify_url() {
            return notify_url;
        }

        public void setNotify_url(String notify_url) {
            this.notify_url = notify_url;
        }

        public String getTrade_type() {
            return trade_type;
        }

        public void setTrade_type(String trade_type) {
            this.trade_type = trade_type;
        }

        public String getOpenid() {
            return openid;
        }

        public void setOpenid(String openid) {
            this.openid = openid;
        }

        public String toXmlString() {
            String xml = "<appid>" + getAppid() + "</appid>"
                    + "<attach>" + getAttach() + "</attach>"
                    + "<body>" + getBody() + "</body>"
                    + "<device_info>" + getDevice_info() + "</device_info>"
                    + "<mch_id>" + getMch_id() + "</mch_id>"
                    + "<nonce_str>" + getNonce_str() + "</nonce_str>"
                    + "<notify_url>" + getNotify_url() + "</notify_url>"
                    + "<openid>" + getOpenid() + "</openid>"
                    + "<out_trade_no>" + getOut_trade_no() + "</out_trade_no>"
                    + "<spbill_create_ip>" + getSpbill_create_ip() + "</spbill_create_ip>"
                    + "<total_fee>" + getTotal_fee() + "</total_fee>"
                    + "<trade_type>" + getTrade_type() + "</trade_type>";

            return xml;
        }
    }

    public PayInfo createPayInfo(String appid, String mch_id, String orderSn, String openId) {
        PayInfo payInfo = new PayInfo();
        payInfo.setAppid(appid);
        payInfo.setDevice_info("WEB");
        payInfo.setMch_id(mch_id);
        payInfo.setNonce_str("123456");
        payInfo.setBody("零用卡支付");
        payInfo.setAttach(orderSn);
        payInfo.setOut_trade_no(orderSn);
        payInfo.setTotal_fee(1);
        payInfo.setSpbill_create_ip("");
        payInfo.setNotify_url("http://fredia.natapp1.cc/pay/pay_callback");
        payInfo.setTrade_type("JSAPI");
        payInfo.setOpenid(openId); //o8bjIwMi0h4osQC0HHw0-y75XjY8
        return payInfo;
    }

    public String getSign(PayInfo payInfo) throws Exception {
        String signTemp = "appid=" + payInfo.getAppid()
                + "&attach=" + payInfo.getAttach()
                + "&body=" + payInfo.getBody()
                + "&device_info=" + payInfo.getDevice_info()
                + "&mch_id=" + payInfo.getMch_id()
                + "&nonce_str=" + payInfo.getNonce_str()
                + "&notify_url=" + payInfo.getNotify_url()
                + "&openid=" + payInfo.getOpenid()
                + "&out_trade_no=" + payInfo.getOut_trade_no()
                + "&spbill_create_ip=" + payInfo.getSpbill_create_ip()
                + "&total_fee=" + payInfo.getTotal_fee()
                + "&trade_type=" + payInfo.getTrade_type()
                + "&key=" + "moyestore12345678901234567890123"; //这个key注意

        String sign = Md5Utils.getMD5(signTemp);
        return sign;
    }


}
